package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 搜狐博客解析
 * @author KK
 *
 */
public class SohuBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		if(url.startsWith("http://blog.sohu.com/list/")){
			return false;
		}
		String[] regex = {
				"http://[a-z0-9-]*.i.sohu.com/blog/view/[0-9]*.htm",
				"http://blog.sohu.com/.*/.*.html",
				"http://.*.blog.sohu.com/[0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		String author=null;
		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("title").text();
		if (StringUtils.isNotEmpty(title)){
			title = StringUtils.substringBeforeLast(title, "-");
			title = StringUtils.substringBeforeLast(title, "-");
		}

		content = HtmlCleaner.getContentHtml(url,  doc.select("div#blogarticlefont"));

		if (StringUtils.isNullOrEmpty(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("div#main-content"));
		}

		author = doc.select("div#blogUrl a.navUrl").attr("title");
		author = StringUtils.substringBetween(author, "查看", "的博客");
		if(StringUtils.isBlank(author)){
			author = doc.select("div#p-username").text();
		}

		date = DateUtils.matchDate(doc.select(".date").text());
		if (date==null){
			date = DateUtils.matchDate(doc.select("div.item-title").text());
			if (date==null){
				date = DateUtils.matchDate(doc.select("div.date").text());
				if (date==null){
					date = DateUtils.matchDate(doc.select("span.date").text());
				}
			}

		}

		NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setAuthor(author);
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
				return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null) {
			String updateUrl = meta.getUpdateUrl();
			if(StringUtils.isNotBlank(updateUrl)){
				String[] split = updateUrl.split("###");
				String clickUrl=split[0];
				String commentUrl=split[1];
				if(!"NULL".equals(clickUrl)){
					String html = CrawlHTML.getHtml(clickUrl, "utf8", "");
					if(html!=null){
						Integer clickNum = NumberUtils.matchNumber(StringUtils.substringBetween(html, ":", "}"));
						meta.setClickNum(clickNum );
					}
				}
				if(!"NULL".equals(commentUrl)){
					String html = CrawlHTML.getHtml(commentUrl, "utf8", "");
					if(html!=null){
						Integer commentNum = NumberUtils.matchNumber(StringUtils.substringBetween(html, "discusscount\":", ","));
						meta.setCommentNum(commentNum);
					}
				}
			}
			return meta;
		}
		return null;
	}




	
/*
	public static void main(String[] args) {
		String url = "http://gudengtubi.blog.sohu.com";
//		String url = "http://blog.sohu.com/people/!NjU5NTEzMEI2MTdBNkE2Njg4M0I3QTI3RjI4NDEzM0FAcXEuc29odS5jb20=/252144632.html";
//		String url = "http://caoying1.blog.sohu.com/110223056.html";
		SohuBlogAnalyse test = new SohuBlogAnalyse();
//		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			System.out.println(test.getLinks(responseToURL));
//			NewsMeta parserHtml = test.parserHtml(responseToURL);
//			System.out.println(parserHtml);
//		}else{
//			System.out.println("不符合规则");
//		}
	}


	public static HashMap<String, String> getLinks(UrlMeta urlMeta) {
		String html = urlMeta.getHtml();
//		System.out.println(html);
		if(html!=null && html.contains("var _ebi =")){
			 HashMap<String, String> map = new HashMap<String, String>();
			 String id = StringUtils.substringBetween(html, "var _ebi =",";");
			 if(id!=null) id = id.replace("'", "");
			 if(id!=null) id = id.replace(" ", "");
			 String url = urlMeta.getUrl() + "/sff/entries/" + id + ".html"	;
			 UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			 return CrawlHTML.getCommonLinkListByHtml(responseToURL);
//			 System.out.println(id);
		}
		return null;
	}
	*/
}
